function [ev_times] = find_artifacts(volt, dfile, rec, evthresh)
%
% field potential artifact suppressor.
% "Automatic" artifact suppressor. 
% This routine looks for dv/dt > 1000 mv/ms, and saves those into stim_time.
% it then does the same as FP_artsupp (in fact, it calls that routine). 
% The "deadwin" is to suppress artifcat in data set prior to
% filtering and analysis...
%
% 12/10/01 Paul B. Manis.
% 6/2/2008 Paul B. Manis (auto artifact detection). 
% finds artifacts and stores result into stim_time
% 7/4/2008 if rec is a list, the ev_times is a cell array with entries for
% each member of the list.

if(nargin < 4 || evthresh == 0)
    evthresh = 18000;
end;

ev_times = {};
sign = 1;
vfp =  volt;
tm = make_time(dfile); % get the time base
dtm = diff(tm(1,:));


ev_pos = [];
k=1;
for i = rec
    dvfp = diff(volt(i,:))./dtm; % compute derivative
    k2=zeros(1, size(volt, 1)); % initialize pulse vector
   if(sign > 0)
      [d1, k2] = find(dvfp > evthresh); % find points > threshold
      switch(length(k2))
      case 0
         ev_pos = [];
      case 1
         ev_pos = k2(1);
      otherwise
         ev_pos = [k2(1) k2(find(diff(k2) > 2)+1)]; % and store the resulting array
      end;
      
   else
      [d1, k2] = find(dvfp < evthresh); % find points < threshold
      switch(length(k2))
      case 0
         ev_pos = [];
      case 1
         ev_pos = k2(1);
      otherwise
         ev_pos = [k2(1) k2(find(diff(k2) >2)+1)]; % and store the resulting array
      end;
   end;
    ev_times{k} = tm(i, ev_pos);
    k = k + 1;
end   
%fprintf(1, '# suppressed: %d\n', length(ev_times));
%ev_times
